import java.util.Calendar;

//
// Store all of the data for a particular satellite 
// in this case
//

// Only create a new class when a satelite is observed for the first time
public class dsSatelliteData {

    // Total number of epochs in the RINEX file being parsed
    private int totalEpochs; 

    private static String glonassConstell ="R";
    private static String gpsConstell     ="G";
    private int numGlonassSats     = 0;
    private int numGPSSats         = 0;
    private int numGallileoSats    = 0;

    // An array of Satellite Data Objects
    private SatelliteData[] satData;
    private int i;

    dsSatelliteData ( int totalSats, int tEpochs ) {
        satData = new SatelliteData[totalSats];
        totalEpochs = tEpochs;
        i = 0;
    }

    // Create a new Satellite Data Object
    // Satellite PRN number reported in RINEX file
    // Constell -> satelite constellation 'G', 'R', 
    public void newSat( int PRN, String constell, int numEpochs, int numObsTypes ) {
        satData[i] = new SatelliteData( PRN, constell, numEpochs, numObsTypes );
        i++;

        // Keep track of how many we track from each constellation
        if ( constell.matches(gpsConstell) ) {
            numGlonassSats++;
        }
        else if (constell.matches(glonassConstell) ) {
            numGlonassSats++;
        }
        else { // This is for the case when the constellation is not stated
            numGPSSats++;
        }
    }
   
    public int addData(int PRN,String constell,double data,int index) {
        //System.out.println("About to add data to :"+PRN+":"+constell+":"+data+":"+index);
        int j = 0;
        j = _findSat(PRN,constell);

        satData[j].addData(data, index);
        return 1;
    }

    public int setTime(int PRN,String constell,long time) {
        int j = 0;
        j = _findSat(PRN,constell);
        satData[j].setTime(time);
        return 1;
    }

    //
    // Increment the number of epochs
    //  This is called after thelast observation is read/parsed into the Satellite 
    //  Data Class
    //
    public void incEpoch(int PRN,String constell) { 
        int j = 0;
        j = _findSat(PRN,constell);
        satData[j].incEpoch();
        //satData[j].setEpoch();
    }

    // Print all of the SNR data for the requested satellite to the screen
    public void dumpObs( int PRN, String constell, int index ) {
        int j = 0;
        j = _findSat(PRN, constell);
        satData[j].dumpObs(index);
    }

    private int _findSat(int PRN, String constell) {
        //System.out.println("Checking sat"+PRN+"Constell:"+constell);
        for(int j =0 ; j < satData.length ; j++ ) {
            int outPRN = satData[j].getPRN() ;
            //System.out.println(j+" We have PRN: "+outPRN);
            if(satData[j].getPRN() == PRN ) {
                //Now check the constellations match
                String tConstell = satData[j].getConstell();
                //System.out.println("Checking sat"+PRN+"Constell:"+constell);
                if(tConstell.matches(constell) ) {
                    return j;
                }
            }
        }
        System.err.println("Satellite Does not Exists PRN:"+PRN+": Constell "+constell);
        return -1;
    }
 
    public void dumpSatPrns() {
        for(int j =0 ; j < satData.length ; j++ ) {
            System.out.println(j+" PRN: "+satData[j].getPRN() );
        } 
    }
    
    public double getP1(int PRN, int epoch) {
        int j = 0;
        j = _findSat(PRN, "G");
        //System.out.println("getP1: "+j);
        double P1 = satData[j].getP1(epoch);
        return P1;
    } 
    
    public long getTime(int PRN, int index) {
        int j = 0;
        j = _findSat(PRN, "G");
        System.out.println("getP1: "+j);
        long time = satData[j].getTime(index);
        return time;
    } 
  /* 
    public static void dumpGlonassSats() {
        for(int j =0 ; j < satData.length ; j++ ) {
            String tConstell = satData[j].getConstell();
            if(tConstell.matches(glonassConstell) ) {
                System.out.print(satData[j].getPRN()+" ");
            }
        }
        System.out.println("");
    } 
*/
}
